package com.hongxun.design.acceptance.view;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.acceptance.mode.problem;
import com.hongxun.design.acceptance.mode.problemPhoto;
import com.hongxun.design.mytools.MString;
import com.hongxun.design.mytools.ShardPreferencesTool;
import com.hongxun.design.utils.StringUtil;
import com.hongxun.design.utils.imageutil.UriUtil;

import java.util.List;

/**
 * Created by cqcqwucp 照片设置缺陷位置
 */

public class PopWaterMark extends PopupWindow {
    private Activity activity;
    private problem pro;
    private List<problemPhoto> photos;

    /**
     * 0Activity
     * 1Handler
     * 2Obj
     */
    public PopWaterMark(Object... obj) {
        this.activity = (Activity) obj[0];
        this.pro = (problem) obj[1];
        String readLib = ShardPreferencesTool.read(activity.getApplicationContext(),MString.libraryPhoto, "",1);
        this.photos = JSON.parseArray(readLib, problemPhoto.class);

        View view = LayoutInflater.from(activity).inflate(R.layout.pop_recdetail, null);
        final ViewHolder holder = new ViewHolder(view);
        setContentView(view);
        setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
        setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        setBackgroundDrawable(dw);
        setAnimationStyle(R.style.pop_r_anim);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        holder.setData();
    }

    class ViewHolder {
        private TextView tvFirst, fx1, fx2, fx3, fx4;
        private LinearLayout layphotos;
        private ImageView back;

        ViewHolder(View view) {
            tvFirst = (TextView) view.findViewById(R.id.tv_title1);
            fx1 = (TextView) view.findViewById(R.id.tv_fx1);
            fx2 = (TextView) view.findViewById(R.id.tv_fx2);
            fx3 = (TextView) view.findViewById(R.id.tv_fx3);
            fx4 = (TextView) view.findViewById(R.id.tv_fx4);
            layphotos = (LinearLayout) view.findViewById(R.id.layout_photos);
            back = (ImageView) view.findViewById(R.id.iv_back);
        }

        private void setData() {
            back.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            String fir = "常见问题：" + "<font color='#FF0000'>" + pro.getBigType() + "-" + pro.getSmallType() + "</font>";
            tvFirst.setText(Html.fromHtml(fir));
            fx1.setText(pro.getCommon());
            fx2.setText(pro.getAnalysis());
            fx3.setText(pro.getStandard());
            fx4.setText(pro.getMeasures());
            //加载服务器图片
            layphotos.removeAllViews();
            if (photos != null) {
                ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(240, -2);
                for (problemPhoto photo : photos) {
                    if (pro.getGuid().equals(StringUtil.getNull(photo.getProblemGuid()))) {
                        ImageView view = new ImageView(activity);
                        view.setScaleType(ImageView.ScaleType.FIT_XY);
                        view.setImageURI(UriUtil.getImgFromService(StringUtil.getNull(photo.getComputerUrls())));
                        layphotos.addView(view, 0, params);
                    }
                }
            }
        }
    }
}
